package cn.iocoder.yudao.module.wecom.service.cusfollowuser;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.wecom.dal.dataobject.user.WeUserDO;
import cn.iocoder.yudao.module.wecom.dal.mysql.user.WeUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.wecom.controller.admin.cusfollowuser.vo.*;
import cn.iocoder.yudao.module.wecom.dal.dataobject.cusfollowuser.WeCusFollowUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wecom.dal.mysql.cusfollowuser.WeCusFollowUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.module.wecom.enums.ErrorCodeConstants.*;

/**
 * 客户跟进人 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class WeCusFollowUserServiceImpl implements WeCusFollowUserService {

    @Resource
    private WeCusFollowUserMapper weCusFollowUserMapper;

    @Resource
    private WeUserMapper weUserMapper;

    @Override
    public PageResult<WeCusFollowUserDO> getWeCusFollowUserPage(WeCusFollowUserPageReqVO pageReqVO) {
        return weCusFollowUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WeCusFollowUserDO> getWeCusFollowUserListByCustomerIds(Collection<Long> ids) {
        return weCusFollowUserMapper.selectList(new LambdaQueryWrapperX<WeCusFollowUserDO>().in(WeCusFollowUserDO::getCustomerId, ids).orderByAsc(WeCusFollowUserDO::getCreateTime));
    }



    @Override
    public Map<Long, List<WeCusFollowUserRespVO>> getWeCusFollowUserMap(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyMap();
        }
        List<WeCusFollowUserDO> list = getWeCusFollowUserListByCustomerIds(ids);
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyMap();
        }

        List<WeUserDO> userDOS = weUserMapper.selectList(new LambdaQueryWrapperX<WeUserDO>().in(WeUserDO::getWeUserId, list.stream().map(WeCusFollowUserDO::getQwUserId).collect(Collectors.toList())));

        Map<String, WeUserDO> map = convertMap(userDOS, WeUserDO::getWeUserId);

        Map<Long, List<WeCusFollowUserRespVO>> result = new HashMap<>();

        list.forEach(data -> {
            if (result.containsKey(data.getCustomerId())) {
                List<WeCusFollowUserRespVO> cache = result.get(data.getCustomerId());
                cache.add(convertToResp(data, map));
                result.put(data.getCustomerId(), cache);

            } else {
                result.put(data.getCustomerId(), ListUtil.toList(convertToResp(data, map)));
            }
        });
        return result;
    }

    public WeCusFollowUserRespVO convertToResp(WeCusFollowUserDO userDO, Map<String, WeUserDO> map) {
        WeCusFollowUserRespVO respVO = BeanUtils.toBean(userDO, WeCusFollowUserRespVO.class);
        MapUtils.findAndThen(map, userDO.getQwUserId(), (s) -> respVO.setUserNickname(s.getNickname()));
        return respVO;
    }
}